import { createRouter, createWebHistory } from 'vue-router'
import { useTokenStore } from '@/stores/token.js'
import Login from '../views/login.vue'
import Register from '../views/register.vue'
import Home from '../views/home.vue'
import Profile from '../views/profile.vue'
import EventsList from '../views/events-list.vue'
import NotificationsList from '../views/notifications-list.vue'
import NewsList from '../views/news-list.vue'
import EventDetail from '../views/event-detail.vue'
import NotificationDetail from '../views/notification-detail.vue'
import NewsDetail from '../views/news-detail.vue'
import TeacherList from '../views/teacher-list.vue'

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      title: '学生登录 - 校园智慧竞赛平台'
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta: {
      title: '学生注册 - 校园智慧竞赛平台'
    }
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta: {
      title: '主页大厅 - 校园智慧竞赛平台'
    }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: Profile,
    meta: {
      title: '个人中心 - 校园智慧竞赛平台'
    }
  },
  {
    path: '/events-list',
    name: 'EventsList',
    component: EventsList,
    meta: {
      title: '近期赛事 - 校园智慧竞赛平台'
    }
  },
  {
    path: '/notifications-list',
    name: 'NotificationsList',
    component: NotificationsList,
    meta: {
      title: '最新通知 - 校园智慧竞赛平台'
    }
  },
  {
    path: '/news-list',
    name: 'NewsList',
    component: NewsList,
    meta: {
      title: '新闻资讯 - 校园智慧竞赛平台'
    }
  },
  {
    path: '/event-detail/:id',
    name: 'EventDetail',
    component: EventDetail,
    meta: {
      title: '赛事详情 - 校园智慧竞赛平台'
    }
  },
  {
    path: '/notification-detail/:id',
    name: 'NotificationDetail',
    component: NotificationDetail,
    meta: {
      title: '通知详情 - 校园智慧竞赛平台'
    }
  },
  {
    path: '/news-detail/:id',
    name: 'NewsDetail',
    component: NewsDetail,
    meta: {
      title: '新闻详情 - 校园智慧竞赛平台'
    }
  },
  {
    path: '/teacher-list',
    name: 'TeacherList',
    component: TeacherList,
    meta: {
      title: '教师队伍 - 校园智慧竞赛平台'
    }
  },
  {
    path: '/teacher-detail/:employeeId',
    name: 'TeacherDetail',
    component: () => import('../views/teacher-detail.vue'),
    meta: {
      title: '教师详情 - 校园智慧竞赛平台'
    }
  },
  {
    path: '/team-detail/:teamId',
    name: 'TeamDetail',
    component: () => import('../views/team-detail.vue'),
    meta: {
      title: '队伍详情 - 校园智慧竞赛平台'
    }
  },
  {
    path: '/notification-center/:teamId',
    name: 'NotificationCenter',
    component: () => import('../views/notification-center.vue'),
    meta: {
      title: '通知中心 - 校园智慧竞赛平台'
    }
  },
  {
    path: '/publish-notification/:teamId',
    name: 'PublishNotification',
    component: () => import('../views/publish-notification.vue'),
    meta: {
      title: '发布通知 - 校园智慧竞赛平台'
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title
  }

  // 检查登录状态
  const tokenStore = useTokenStore()

  // 如果访问的不是登录或注册页面，且没有token，则跳转到登录页
  if (to.path !== '/login' && to.path !== '/register' && !tokenStore.token) {
    next('/login')
    return
  }

  // 如果已登录且访问登录页，但不是从注册页面来的，才跳转到主页
  // 允许用户从注册页面主动返回登录页面
  if (to.path === '/login' && tokenStore.token && from.path !== '/register') {
    next('/home')
    return
  }


  next()
})

export default router